package com.dianjing.dianqun.api.dianpai;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.model.Content;
import com.dianjing.dianqun.model.Site;
import com.dianjing.dianqun.service.ChannelService;
import com.dianjing.dianqun.service.ContentService;
import com.dianjing.dianqun.service.SiteService;
import com.dianjing.dianqun.service.UserService;
import com.dianjing.dianqun.service.dianpai.DianPaiContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @date : 2017年11月10日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@Api(description = "搜索相关", value = "SearchApiController")
@RestController
@RequestMapping("/api/search")
public class SearchApiController extends BaseRestController {
	@Autowired
	private UserService userService;

	@Autowired
	private ChannelService channelService;

	@Autowired
	private DianPaiContentService dianPaiContentService;

	@Autowired
	private ContentService contentService;

	@Autowired
	private SiteService siteService;

	@ApiOperation(notes = "根据关键字和站点id（可以为空，则搜索全部站点）搜索文章", value = "根据关键字和站点id（可以为空，则搜索全部站点）搜索文章")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "siteId", required = false, paramType = "query", dataType = "int", value = "站点id "),
			@ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页多少记录", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "pageNum", value = "当前页", paramType = "query"),
			@ApiImplicitParam(name = "keyword", required = true, paramType = "query", dataType = "string", value = "搜索关键字") })
	@GetMapping("/condition")
	public ResponseData search(@RequestParam(value = "siteId", required = false) Integer siteId,
			@RequestParam(value = "keyword", required = true) String keyword,
			@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
		Site site = siteService.selectByPrimaryKey(siteId);
		List<Content> list = null;
		PageHelper.startPage(pageNum, pageSize);
		if (null == site) {
			list = contentService.selectContentByKeyowrd(keyword);
			for (Content content : list) {
				Site site2 = siteService.selectByPrimaryKey(content.getSiteId());
				if (site2 != null)
					content.setSearchName(site2.getSiteName());
			}
		} else {
			list = contentService.selectContentBySiteAndKeyWord(siteId, keyword);
			for (Content content : list) {
				content.setSearchName(
						channelService.selectByPrimaryKey(content.getChannelId()).getChannelExt().getChannelName());
			}
		}
		PageInfo<Content> page = new PageInfo<Content>(list);
		return new ResponseData(ResponseData.STATUS_OK, "搜索完成", page);
	}

}
